<?php

namespace app\model;

use support\Db;
use support\Model;

class WaApproveSetting
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_approve_setting';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all()
    {
        $list = Db::table('wa_approve_setting')
            ->select('*')
            ->whereIn('status',[0,1])
            ->get();
        if ($list){
            foreach ($list as $key){
                $key->approve_flow = json_decode($key->approve_flow);
                if ($key->approve_flow){
                    foreach ($key->approve_flow as $ke){
                        if ($ke->approve_flow_by_initiator){
                            foreach ($ke->approve_flow_by_initiator as $k){
                                $k->approve_admin_id = $k->approve_admin_id;
                            }
                        }
                    }
                }
            }
        }

        return json_encode($list);
    }


    public static function find($condition)
    {
        $res = Db::table('wa_approve_setting')
            ->where($condition)
            ->first();
//            $key->approve_flow = json_decode($key->approve_flow);
            if ($res->approve_flow){
                $res->approve_flow = json_decode($res->approve_flow);
//                foreach ($res->approve_flow as $ke){
//                    if ($ke->approve_flow_by_initiator){
//                        foreach ($ke->approve_flow_by_initiator as $k){
//                            $k->approve_admin_id = $k->approve_admin_id;
//                        }
//                    }
//                }
            }
        return json_encode($res);
    }

    public static function created($data){

        return Db::table('wa_approve_setting')->insertGetId($data);

    }

    public static function updated($condition,$data){

        $res = Db::table('wa_approve_setting')->where($condition)->update($data);
        return json_encode($res);

    }


}